package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.ProductMapper;
import org.linlinjava.litemall.db.domain.Product;
import org.linlinjava.litemall.db.domain.ProductExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;

@Service
public class ProductService {


    private static Logger logger= LoggerFactory.getLogger(ProductService.class);

    @Resource
    private ProductMapper ProductMapper;

    public int updateById(Product brand) {
        brand.setUpdateTime(LocalDateTime.now());
        return ProductMapper.updateByPrimaryKeySelective(brand);
    }

    public void deleteById(Integer id) {
        ProductMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(Product brand) {
        LocalDateTime now = LocalDateTime.now();
        brand.setAddTime(now);
        brand.setUpdateTime(now);
        ProductMapper.insertSelective(brand);
    }

    public List<Product> all() {
        ProductExample example = new ProductExample();
        example.or().andDeletedEqualTo(false);
        return ProductMapper.selectByExample(example);
    }

    public List<Product> query(Integer page, Integer limit, String sort, String order) {
        ProductExample example = new ProductExample();
        example.or().andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        return ProductMapper.selectByExample(example);
    }

    public List<Product> query(Integer page, Integer limit) {
        return query(page, limit, "add_time", "desc");
    }

    public Product findById(Integer id) {
        return ProductMapper.selectByPrimaryKey(id);
    }

    public List<Product> querySelective(String id, Long startTime,Long endTime,String name, Integer page, Integer size, String sort, String order) {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.valueOf(id));
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        System.out.println(startTime+"  "+endTime);
        if (!ObjectUtils.isEmpty(startTime)&&!ObjectUtils.isEmpty(endTime)) {
            LocalDateTime start = new Date(startTime).toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
            LocalDateTime end = new Date(endTime).toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
            logger.info("{},{}",start,end);
            System.out.println("start="+start+"   endTime="+end);
            criteria.andAddTimeBetween(start,end);
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return ProductMapper.selectByExample(example);
    }

    public boolean checkExistByName(String name) {
        ProductExample example = new ProductExample();
        example.or().andNameEqualTo(name).andDeletedEqualTo(false);
        return ProductMapper.countByExample(example) != 0;
    }

    public Long count() {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        return ProductMapper.countByExample(example);
    }
}
